
#import "ReadViewController.h"
#import "ContentViewController.h"
#import "ViewController.h"

@interface ReadViewController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
{
    BOOL show;
    UIPageViewController *pageViewCon;
//    UIToolbar *toolBar;
    
    CGFloat totalHeight;
    NSUInteger totalPages;
    NSUInteger curPage;
    NSUInteger lenPerPage;
    
    UIBarButtonItem *barItem;
    BOOL iftap;
    UIToolbar *tool;
    UIView *navgationView;
}
@property (nonatomic,strong)ContentViewController *con;
@end

@implementation ReadViewController

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prefersStatusBarHidden];
    iftap = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(onClick:)];
    
    //初始化pageViewCon TransitionStyle 过去动画风格
    pageViewCon = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionSpineLocationKey : @(UIPageViewControllerSpineLocationNone)}];
    
    pageViewCon.dataSource = self;
    pageViewCon.delegate = self;
    [self addChildViewController:pageViewCon];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap)];
    
    [pageViewCon.view addGestureRecognizer:tapGesture];
    

//    pageViewCon.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 44);

    [self.view addSubview:pageViewCon.view];
    
        tool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, pageViewCon.view.frame.size.height - 50, pageViewCon.view.frame.size.width, 50)];
   
    UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithTitle:@"关灯拉闸" style:UIBarButtonItemStylePlain target:self action:@selector(finished:)];
    btn.tag = 1;
    tool.items = @[btn];
    [GlobalData sharedInstance].currentTag = btn.tag;
    
    
    navgationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    navgationView.backgroundColor = [UIColor blackColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(20, 17, 40, 30);
    button.tag = 0;
    [button setTitle:@"返回" forState: UIControlStateNormal];
    [button addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside];
    [navgationView addSubview:button];
    
    
    CGRect rect = [self.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[[UILabel new].font fontWithSize:24.0]} context:nil];
    totalHeight = ceil(rect.size.height);
    totalPages = ceil(totalHeight / (self.view.frame.size.height));
    lenPerPage = _text.length / totalPages;
    curPage = 0;
    


    _con = [ContentViewController new];
    show = YES;
    _con.bagColor = show;
    _con.text = [_text substringWithRange:NSMakeRange(curPage * lenPerPage, lenPerPage)];
    [pageViewCon setViewControllers:@[_con] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
//    [super viewWillAppear:animated];
//  
//    self.navigationController.tabBarController.tabBar.hidden = YES;
}

- (void)finished:(UIButton *)sender{
    
    if (sender.tag) {
        for (UIView *tmpView in pageViewCon.view.subviews) {
            for (UIView *subTmpV in tmpView.subviews) {
                if ([subTmpV isKindOfClass:[UITextView class]]) {
                    UITextView *textV = (UITextView *)subTmpV;
                    textV.backgroundColor = [UIColor blackColor];
                    textV.textColor = [UIColor whiteColor];
                }
            }
        }
        
        sender.tag = 0;
    }else{
        for (UIView *tmpView in pageViewCon.view.subviews) {
            for (UIView *subTmpV in tmpView.subviews) {
                if ([subTmpV isKindOfClass:[UITextView class]]) {
                    UITextView *textV = (UITextView *)subTmpV;
                    textV.backgroundColor = [UIColor whiteColor];
                    textV.textColor = [UIColor blackColor];
                }
            }
        }
        sender.tag = 1;
    }
    [GlobalData sharedInstance].currentTag = sender.tag;
    
}

- (void)onClick:(id)sender
{

}

- (void)gotoLast:(id)sender
{
    curPage = totalPages - 1;
    ContentViewController *con = [self getCurContentController];
    [pageViewCon setViewControllers:@[con] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    
    
    [self refreshBarItemShow];
}

- (void)refreshBarItemShow
{
    barItem.title = [NSString stringWithFormat:@"第%u页／共%u页",curPage + 1, totalPages];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if (curPage <= 0) {
        return nil;
    }
    curPage--;
    [self refreshBarItemShow];
    return [self getCurContentController];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    if (curPage >= totalPages - 1) {
        return nil;
    }
    curPage++;
    [self refreshBarItemShow];
    return [self getCurContentController];
}

- (ContentViewController *)getCurContentController
{
    ContentViewController *con = [ContentViewController new];
    con.text = [_text substringWithRange:NSMakeRange(curPage * lenPerPage, lenPerPage)];
        con.index = curPage;
    return con;
}

- (void)onTap
{
   // NSLog(@"ON TAP!!!!!");
    if(!iftap)
    {
        iftap = YES;
        [self.view addSubview:tool];
        [self.view addSubview:navgationView];
     
    }
    else {
        iftap = NO;
        [tool removeFromSuperview];
        [navgationView removeFromSuperview];
    }
}

- (void)onBack
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed
{
//    NSLog(@"animate finished? %d, trans completed? %d", finished, completed);
    if (!completed) {
        ContentViewController *con = (ContentViewController *)previousViewControllers[0];
        curPage = con.index;
        [self refreshBarItemShow];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
